package com.cqgc.controller;
import com.cqgc.pojo.dto.ShoppingCartDTO.ShoppingCartDTO;
import com.cqgc.pojo.dto.ShoppingCartDTO.ShoppingCartDeleteDTO;
import com.cqgc.pojo.dto.ShoppingCartDTO.ShoppingCartPageDTO;
import com.cqgc.pojo.entity.PageResult;
import com.cqgc.pojo.entity.Result;
import com.cqgc.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/shoppingCart")
@Api(tags = "购物车接口")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;
    @PostMapping
    @ApiOperation("增加商品购物车")
    @RequiresRoles(value = {"purchaser","admin"} ,logical = Logical.OR)
    public Result addShoppingCart(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        shoppingCartService.addShoppingCart(shoppingCartDTO);
        return Result.success("添加购物车成功");
    }



    @GetMapping
    @ApiOperation("购物车列表")
    @RequiresRoles(value = {"purchaser","admin"} ,logical = Logical.OR)
    public Result<PageResult> list(ShoppingCartPageDTO shoppingCartPageDTO) {
        return Result.success(shoppingCartService.list(shoppingCartPageDTO));
    }



    @DeleteMapping
    @ApiOperation("清空购物车")
    @RequiresRoles(value = {"purchaser","admin"} ,logical = Logical.OR)
    public Result cleanShoppingCart() {
        shoppingCartService.cleanShoppingCart();
        return Result.success("清空购物车成功");
    }

    @DeleteMapping("/{gid}")
    @ApiOperation("将一个商品清除购物车")
    @RequiresRoles(value = {"purchaser","admin"} ,logical = Logical.OR)
    public Result deleteByGid(@PathVariable int gid) {
        shoppingCartService.deleteByGid(gid);
        return Result.success("删除购物车成功");
    }

    @PutMapping
    @ApiOperation("修改购物车")
    @RequiresRoles(value = {"purchaser","admin"} ,logical = Logical.OR)
    public Result updateQuantity(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        shoppingCartService.updateQuantity(shoppingCartDTO);
        return Result.success("修改购物车成功");
    }


    @PostMapping("/deleteByGids")
    @ApiOperation("批量删除购物车")
    public Result deleteByGids(@RequestBody ShoppingCartDeleteDTO shoppingCartDeleteDTO){
        int row = shoppingCartService.deleteByGids(shoppingCartDeleteDTO);
        if (row!=0) {
            return Result.success("删除购物车成功");
        }
        return Result.error("删除购物车失败");
    }







}
